package com.example.mygrade.webapi;

import com.example.mygrade.core.RUtil;
import com.example.mygrade.domain.Student;
import com.example.mygrade.domain.Teacher;
import com.example.mygrade.domain.TeacherMa;
import com.example.mygrade.intercepter.GlobalConstant;
import com.example.mygrade.service.TeacherMaService;
import com.example.mygrade.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/webapi/update")
public class TeacherMaRestController {
    @Autowired
    TeacherMaService teacherMaService;

    /**
     * 修改密码
     * @param userName
     * @param oldpassword
     * @param newpassword
     * @return
     */
    @PostMapping("/password")
    public R changePass(String userName,String oldpassword,String newpassword){
        TeacherMa teacherMa = teacherMaService.findByMnameLike(userName);
        if (teacherMa == null){
            return RUtil.error(500,"用户不存在！");
        }
        if (!oldpassword.equals(teacherMa.getMpass())){
            return RUtil.error(400,"旧密码错误");
        }
        teacherMa.setMpass(newpassword);
        TeacherMa teacherMa1 = teacherMaService.update(teacherMa);
        return RUtil.success(teacherMa1,"修改成功");
    }

    /**
     * 获取管理员信息
     * @param httprequest
     * @return
     */
    @GetMapping("/queryinfo")
    public R queryInfo(HttpServletRequest httprequest){
        HttpSession session = httprequest.getSession();
        Object obj = session.getAttribute(GlobalConstant.MANAGE_SESSION_KEY.getMsg());
        TeacherMa teacherMa = (TeacherMa) obj;
        return RUtil.success(teacherMa,"获取成功");
    }
}
